package modellayer;

import java.util.ArrayList;

public class DVDContainer
{
	private static DVDContainer instance;
	private ArrayList<DVD> dvds;

	/**
	 * the constructor of class DVDContainer
	 */
	public DVDContainer()
	{
		dvds = new ArrayList<DVD>();
	}

	/**
	 *Singleton method
	 */
	public static DVDContainer getInstance()
	{
		if(instance == null){
			instance = new DVDContainer();
		}
		return instance;
	}

	/**
	 * @param d DVD
	 * adds a DVD d to the arraylist dvds
	 */
	public void addDVD(DVD d)
	{
		dvds.add(d);
	}

	/**
	 * Iterate through the collection of dvd's, and print each title
	 */
	public void printAll()
	{
		int i = 0;
		int j = 0;
		//While i is less than the collection size, get dvd and print title
		if(dvds.isEmpty())
		{
			System.out.println("You don't have any DVD's!");
		}
		while ( i < dvds.size()) 
		{
			System.out.println(dvds.get(i).getTitle());
			System.out.println("Copies: ");
			while(j < dvds.get(i).getCopies().size())
			{
				System.out.println(dvds.get(i).getCopies().get(j).getSerialNumber() + "(Home: " + dvds.get(i).getCopies().get(j).isHome()+ ")");
				j++;
			}
			i++;	
		}
	}
	/**
	 * Find a specific dvd, by searching through the collection, and return the object
	 * @param title the title
	 * @return the dvd
	 */
	public DVD findDVD(String title)
	{
		int i=0;
		boolean found = false;
		//While i is less than the collection size and found is not true
		while (!found && i < dvds.size()) 
		{       
			//If search title(String) is the same as current dvd's title, set found = true
			if (title.equals(dvds.get(i).getTitle()))
			{
				found = true;
			}
			else i++;
		}
		if (found)
		{
			return dvds.get(i);
		}
		else 
			return null;
	}

	/**
	 * Delete dvd
	 * @param title the title
	 * @return true, if successful
	 */
	public boolean deleteDVD(String title)
	{
		boolean successful = true;
		if(findDVD(title)==null)
		{
			successful = false;
		}
		dvds.remove(findDVD(title));
		return successful;
	}

	/**
	 * @return successful boolean
	 */
	public boolean updateDVD(String oldTitle, String title, String barcode, String publicationDate)
	{
		boolean successful = false;
		DVD d = findDVD(oldTitle);
		d.setTitle(title);
		d.setBarcode(barcode);
		d.setPublicationDate(publicationDate);
		if(d.getTitle().equals(title) && d.getBarcode().equals(barcode) && d.getPublicationDate().equals(publicationDate))
		{
			successful = true;
		}
		return successful;
	}
}